package com.yunji.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunji.news.entity.EnrollmentApplication;
import com.yunji.news.entity.QualificationList;
import com.yunji.news.mapper.QualificationListMapper;
import com.yunji.news.service.EnrollmentApplicationService;
import com.yunji.news.service.QualificationListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 入围名单服务实现类
 *
 * @author yunji
 * @since 2024-11-26
 */
@Service
public class QualificationListServiceImpl extends ServiceImpl<QualificationListMapper, QualificationList> 
        implements QualificationListService {

    @Autowired
    private EnrollmentApplicationService applicationService;

    @Override
    public IPage<QualificationList> getQualificationPage(Integer page, Integer size, 
                                                        Long schoolId, Long majorId, 
                                                        String qualificationType, String keyword) {
        Page<QualificationList> pageParam = new Page<>(page, size);
        return baseMapper.selectQualificationPage(pageParam, schoolId, majorId, qualificationType, keyword);
    }

    @Override
    public boolean generateQualificationList(Long schoolId, Long majorId, String qualificationType, Integer count) {
        // 查询符合条件的申请，按成绩排序
        LambdaQueryWrapper<EnrollmentApplication> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EnrollmentApplication::getSchoolId, schoolId)
               .eq(EnrollmentApplication::getMajorId, majorId)
               .eq(EnrollmentApplication::getApplicationStatus, "approved")
               .eq(EnrollmentApplication::getPaymentStatus, "paid")
               .isNotNull(EnrollmentApplication::getTotalScore)
               .orderByDesc(EnrollmentApplication::getTotalScore)
               .last("LIMIT " + count);

        List<EnrollmentApplication> applications = applicationService.list(wrapper);

        // 生成入围名单
        for (int i = 0; i < applications.size(); i++) {
            EnrollmentApplication app = applications.get(i);
            QualificationList qualification = new QualificationList();
            qualification.setSchoolId(schoolId);
            qualification.setMajorId(majorId);
            qualification.setApplicationId(app.getId());
            qualification.setStudentName(app.getStudentName());
            qualification.setIdCard(app.getIdCard());
            qualification.setTotalScore(app.getTotalScore());
            qualification.setRankInMajor(i + 1);
            qualification.setQualificationType(qualificationType);
            qualification.setStatus(1);
            
            save(qualification);
        }

        return true;
    }

    @Override
    public boolean publishQualificationList(Long[] ids) {
        LambdaUpdateWrapper<QualificationList> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(QualificationList::getId, Arrays.asList(ids))
               .set(QualificationList::getPublishTime, LocalDateTime.now())
               .set(QualificationList::getStatus, 2); // 2: 已发布
        return update(wrapper);
    }

    @Override
    public List<QualificationList> getPublicQualificationList(Long schoolId, Long majorId, String qualificationType) {
        LambdaQueryWrapper<QualificationList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualificationList::getSchoolId, schoolId)
               .eq(schoolId != null, QualificationList::getSchoolId, schoolId)
               .eq(majorId != null, QualificationList::getMajorId, majorId)
               .eq(qualificationType != null, QualificationList::getQualificationType, qualificationType)
               .eq(QualificationList::getStatus, 2)
               .orderByAsc(QualificationList::getRankInMajor);
        return list(wrapper);
    }

    @Override
    public QualificationList getStudentQualification(String idCard, Long schoolId) {
        LambdaQueryWrapper<QualificationList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualificationList::getIdCard, idCard)
               .eq(schoolId != null, QualificationList::getSchoolId, schoolId)
               .eq(QualificationList::getStatus, 2);
        return getOne(wrapper);
    }
}